package com.buric.gateway.config;

import com.github.xiaoymin.knife4j.aggre.spring.configuration.Knife4jAggregationProperties;
import com.github.xiaoymin.knife4j.aggre.spring.support.NacosSetting;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Component;
import springfox.documentation.swagger.web.SwaggerResource;
import springfox.documentation.swagger.web.SwaggerResourcesProvider;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *
 * </p>
 *
 * @author zhou.wei
 * @version 1.0
 * @since 2025/2/11 15:44
 */
@Primary
@Component
public class SwaggerResourceConfig implements SwaggerResourcesProvider {

    private final Knife4jAggregationProperties knife4jAggregationProperties;

    public SwaggerResourceConfig(Knife4jAggregationProperties knife4jAggregationProperties) {
        this.knife4jAggregationProperties = knife4jAggregationProperties;
    }

    @Override
    public List<SwaggerResource> get() {
        List<SwaggerResource> resources = new ArrayList<>();
        NacosSetting nacos = knife4jAggregationProperties.getNacos();
        // resources为所有路由都加载到文档，如果需要部分显示，在下方使用filter进行过滤即可
        nacos.getRoutes().forEach(route -> resources.add(swaggerResource(route.getName(),
                route.getServicePath() + route.getLocation())));
        return resources;
    }

    private SwaggerResource swaggerResource(String name, String url) {
        SwaggerResource swaggerResource = new SwaggerResource();
        swaggerResource.setName(name);
        swaggerResource.setLocation(url);
        swaggerResource.setUrl(url);
        swaggerResource.setSwaggerVersion("2.0");
        return swaggerResource;
    }

}
